<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/27
  Time: 19:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上师文章管理模块</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js" type="text/javascript"></script>
</head>
<body>
    <!--添加表单-->
    <form class="layui-form" id="addForm" lay-filter="addFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">上师文章名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="articleImage"/>
                <input type="text" name="articleName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上师文章图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="addImg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属上师</label>
            <div class="layui-input-block">
                <select name="guruId" id="guru1" lay-filter="guruFilter1">
                    <option>--选择上师--</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传时间</label>
            <div class="layui-inline">
                <input type="text" name="articleDate" class="layui-input" id="addDate">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="articleContent" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addFilter">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <!--添加按钮-->
    <button class="layui-btn" id="add">添加</button>
    <!--展示表格-->
    <table id="myTable" lay-filter="tableFilter"></table>
    <!--表格显示图片-->
    <script type="text/html" id="showImg">
        <img src="${pageContext.request.contextPath}/article/{{d.articleImage}}" height="50px" width="100px"/>
    </script>
    <!--toolbar组件-->
    <script type="text/html" id="bar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" lay-event="delete">删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
        </div>
    </script>
    <!--修改表单-->
    <form class="layui-form" id="updateForm" lay-filter="updateFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">上师文章名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="articleId"/>
                <input type="hidden" name="articleImage"/>
                <input type="text" name="articleName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上师文章图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="updateImg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属上师</label>
            <div class="layui-input-block">
                <select name="guruId" id="guru2" lay-filter="guruFilter2">
                    <option>--选择上师--</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传时间</label>
            <div class="layui-inline">
                <input type="text" name="articleDate" class="layui-input" id="updateDate">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="articleContent" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updateFilter">修改</button>
            </div>
        </div>
    </form>

<!--js代码-->
    <script>
        layui.use(['table','form','laydate','upload','layer'],function () {
            var table = layui.table;
            var form = layui.form;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var layer = layui.layer;
            var $ = layui.$;

            //1、渲染表格
            var tableIns = table.render({
                elem:'#myTable',
                url:'${pageContext.request.contextPath}/cmfzArticle/show',
                page:true,
                limit:3,
                limits:[3,4,5],
                cols:[[
                    {field:'articleId',title:'编号',sort:true,width:120},
                    {field:'articleName',title:'文章名称',sort:true,width:120},
                    {field:'articleImage',title:'文章封面图',templet:'#showImg'},
                    {field:'guruId',title:'所属上师编号',sort:true,width:120},
                    {field:'articleDate',title:'上传日期',sort:true,width:120},
                    {field:'articleContent',title:'内容',sort:true,width:120},
                    {title:'操作',toolbar:'#bar'}
                ]]
            })
            //2、添加操作
            $('#add').click(function () {
                layer.open({
                    type:1,
                    title:'添加页面',
                    area:'900px',
                    content:$('#addForm')
                })
                //2、1加载日期组件
                laydate.render({
                    elem:'#addDate'
                })
                //2、2加载上师下拉框
                $.ajax({
                    url:'${pageContext.request.contextPath}/cmfzGuru/selectAllCmfzGuru',
                    success:function (result) {
                        var s = "";
                        $(result).each(function (i,obj) {
                            s += '<option value="'+obj.guruId+'">'+obj.guruNickname+'</option>';
                            $("#guru1").html(s);
                            //刷新下拉框  因为下拉框数据是动态更新的
                            form.render('select');
                        })
                    }
                })
            })
            //2、3加载图片上传组件
            upload.render({
                elem:'#addImg',
                url:'${pageContext.request.contextPath}/cmfzArticle/upload',
                done:function (result) {
                    if(result.flag){
                        layer.msg('图片上传成功');
                        form.val('addFormFilter',{
                            articleImage:result.fileName
                        })
                    }else{
                        layer.msg('图片上传失败');
                    }
                }
            })
            //2、4提交添加表单
            form.on('submit(addFilter)',function () {
                $.ajax({
                    url:'${pageContext.request.contextPath}/cmfzArticle/add',
                    data:$('#addForm').serialize(),
                    success:function (result) {
                        if(result.flag){
                            layer.msg('添加成功');
                            layer.closeAll();
                            $('#addForm')[0].reset();
                            table.reload('myTable');
                        }else {
                            layer.msg('添加失败');
                        }
                    }
                })
                return false;
            })
            //3.监听toolbar工具栏组件  执行删除和修改操作
            table.on('tool(tableFilter)',function (obj) {
                console.log(obj.data.articleId);
                if(obj.event=='delete'){
                    layer.confirm('确认要删除该项吗？',function (index) {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzArticle/remove',
                            data:'articleId='+obj.data.articleId,
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('myTable');
                                }else {
                                    layer.msg('删除失败');
                                }
                            }
                        })

                        layer.close(index);
                    })
                }else if(obj.event=='update'){
                    //执行修改操作
                    layer.open({
                        type:1,
                        title:'修改页面',
                        area:'900px',
                        content:$('#updateForm')
                    })
                    //加载日期组件
                    laydate.render({
                        elem:'#updateDate'
                    })
                    //加载上师下拉框
                    $.ajax({
                        url:'${pageContext.request.contextPath}/cmfzGuru/selectAllCmfzGuru',
                        success:function (result) {
                            var s = "";
                            $(result).each(function (i,obj) {
                                s += '<option value="'+obj.guruId+'">'+obj.guruNickname+'</option>';
                                $("#guru2").html(s);
                                //刷新下拉框  因为下拉框数据是动态更新的
                                form.render('select');
                            })
                        }
                    })
                    //回显
                    $.ajax({
                        url:'${pageContext.request.contextPath}/cmfzArticle/getOne',
                        data:'articleId='+obj.data.articleId,
                        success:function (result) {
                            form.val('updateFormFilter',{
                                articleId:result.articleId,
                                articleName:result.articleName,
                                articleImage:result.articleImage,
                                articleContent:result.articleContent,
                                articleDate:result.articleDate,
                                guruId:result.guruId
                            })
                        }
                    })
                    //加载图片上传组件
                    upload.render({
                        elem:'#updateImg',
                        url:'${pageContext.request.contextPath}/cmfzArticle/upload',
                        done:function (result) {
                            if(result.flag){
                                layer.msg('图片上传成功');
                                form.val('updateFormFilter',{
                                    articleImage:result.fileName
                                })
                            }else{
                                layer.msg('图片上传失败');
                            }
                        }
                    })
                    //提交更新表单
                    form.on('submit(updateFilter)',function () {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzArticle/update',
                            data:$('#updateForm').serialize(),
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('修改成功');
                                    layer.closeAll();
                                    table.reload('myTable');
                                }else {
                                    layer.msg('修改失败');
                                }
                            }
                        })
                        return false;
                    })

                }
            })


        })
    </script>
</body>
</html>
